﻿<%@ WebHandler Language="C#" Class="ProductPic_Del" %>

using System;
using System.Web;
using Newtonsoft.Json;
public class ProductPic_Del :  IHttpHandler, System.Web.SessionState.IReadOnlySessionState 
{
    
    public void ProcessRequest (HttpContext context) 
    {
        OTS.Common.PageSuccess page = new OTS.Common.PageSuccess();
        if (!OTS.BaseLogin.validataModule(context, (int)OTS.enumSysRight.ProductTypeManager))
        {
            page.success = false;
            page.msg = "非法操作！";

        }
        else
        {
            string _proId = context.Request["productID"];
            string _fileName = context.Request["picName"];
            if (_proId == null || _fileName == null || !OTS.Common.Help.IsNumber(_proId) || _fileName == "")
            {
                page.success = false;
                page.msg = "非法操作！";
            }
            else
            {
                OTS.BLL.ProductBLL bll = new OTS.BLL.ProductBLL();
                string strFiles = bll.GetShowPics(int.Parse(_proId));
                if (strFiles == "")
                {
                    page.success = false;
                    page.msg = "非法操作！";
                }
                else
                {
                    string[] arrFile = strFiles.Split(',');
                    string fName;
                    string strPicNames = "";
                    for (int i = 0; i < arrFile.Length; i++)
                    {
                        fName = System.IO.Path.GetFileNameWithoutExtension(System.Web.HttpContext.Current.Server.MapPath(arrFile[i]));
                        if (fName == _fileName) //找到这个文件
                        { 
                            string strSrcPath = System.Web.HttpContext.Current.Server.MapPath(arrFile[i]);
                            try
                            {
                                System.IO.File.Delete(strSrcPath);
                            }
                            catch { }
                            try
                            { System.IO.File.Delete(PictureProcessing.GetThumbPath(arrFile[i])); }
                            catch { }
                        }
                        else
                        {
                            strPicNames += arrFile[i] + ",";
                        }
                        
                        
                    }
                    if (strPicNames.Length > 0)
                    {
                        strPicNames = strPicNames.Substring(0, strPicNames.Length - 1);
                    }
                    if (bll.UpdateShowPics(int.Parse(_proId), strPicNames))
                    {
                        page.success = true;
                        page.msg = "删除成功！";
                    }
                    else
                    {
                        page.success = false;
                        page.msg = "删除失败！更新失败！";
                    }
                }
            }
            
        }
        context.Response.ContentType = "application/x-json";
        context.Response.Charset = "utf-8";
        context.Response.Write(JavaScriptConvert.SerializeObject(page));
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}